package com.robot.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }

    public void startService(View view) {
        startService(new Intent(this,MyService.class));
    }

    public void stopService(View view) {
        stopService(new Intent(this,MyService.class));
    }

    public void bindService(View view) {
        bindService(new Intent(this,MyService.class),connection, Context.BIND_AUTO_CREATE);
    }

    public void unbindService(View view) {
        unbindService(connection);
    }
    private MyService.DownLoad downLoad;
    //Activity与Service的桥梁
    private ServiceConnection connection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            downLoad=(MyService.DownLoad)service;
            downLoad.startDownload();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
    //一般的写法，当次Activity被销毁的时候，自动解绑服务

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
    }
    //静态发送广播接收者
    public void sendAction1(View view) {
        Intent intent = new Intent();
        // ActionUtils.ACTION_FLAG 与注册时保持一致
        intent.setAction(ActtiomUtil.ACTION_FLAG);
        intent.setPackage(getPackageName());
        sendBroadcast(intent);
    }

    public void sendAction2(View view) {
    }


}